<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
// use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use DB;
use Illuminate\Support\Facades\Redis;
use App\Libs\aliyun\api_demo\SmsDemo;
use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    // use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    // protected $redirectTo = '/articles';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function showLoginForm()
    {
        return view('welcome');
    }

    /*重写父类的login方法 by sen when 2017-05-10*/
    public function login_new(Request $request)
    {
        $is_mobile = DB::table('users')->where('mobile',$request->mobile)->first();
        if(!empty($is_mobile)) {
            $code = Redis::get('mobile_login' . $request->mobile);
            if (!empty($code)) {
                if ($code != $request->sms) {
                    session()->flash('success', '短信验证码错误');
                    return redirect('/');
                }
            } else {
                session()->flash('success', '短信验证码为空或已过期');
                return redirect('/');
            }
            $request->session()->put('mobile', "$request->mobile");
            $status = DB::select("select status from erp_users where mobile='" . $request->mobile . "'");
            $status = $status[0]->status;
            if ($status == 0 || $status == -1) {
                return redirect('/form/1');
            } else if ($status == 1) {
                return redirect('/form/2');
            }else if($status==2){
                return redirect('/users/pass/'.'2');
            } else if($status == 3 ) {
                return redirect('/form/3');
            }else if($status==4){
                return redirect('/users/pass/'.'4');
            } else if ($status == 5 ) {
                return redirect('/users/complete');
            } else if ($status == 6) {
                return redirect('/users/pass2');
            }
        }else{
            session()->flash('success', '手机号错误或手机号不存在');
            return redirect('/');
        }

    }
    //登录短信验证码
    public function login_code(Request $request)
    {
        header('Content-Type: text/plain; charset=utf-8');
        $input = $request->all();
        $phone = $input['mobile'];

        $demo = new SmsDemo(
            APP_KEY,
            APP_SECRET
        );
        $num=rand(100000, 999999);
        $response = $demo->sendSms(
            "不去云门店", // 短信签名
            "SMS_114310021", // 短信模板编号
            "$phone", // 短信接收者
            Array(  // 短信模板中字段的值
                'code' => $num,
            ),
            "123"
        );
        if($response->Code=='OK'){
            //把手机号码存入redis缓存 设置300秒的有效时间
            Redis::sEtex('mobile_login'.$phone,300,$num);
            return response()->json(['status'=>0,'ResultData'=>'成功','info'=>'已发送']);
        }else{
            return response()->json(['status'=>1,'ResultData'=>'失败','info'=>'重复发送']);
        }
    }

}
